package com.th.supcom.pds.prescstatusquery.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.th.supcom.pds.common.client.commom.SimpleMethod;
import com.th.supcom.pds.common.client.model.PdsSupplyDetailModel;
import com.th.supcom.pds.common.client.widget.PdsGrid;
import com.th.supcom.pds.common.utils.ClientUtils;

/**
 * 处方状态查询处方明细面板
 * 
 * @author 杨冲
 */
public class SupplyStatusQueryDetailGrid extends LayoutContainer
{

    private static final String MONEY_KEY = "client_money";
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    private PdsGrid <PdsSupplyDetailModel> m_grid;

    /**
     * 构造函数
     */
    public SupplyStatusQueryDetailGrid ()
    {
        initUI ();
    }

    /**
     * 添加结果
     * 
     * @param result
     */
    public void addResult (final List <PdsSupplyDetailModel> result)
    {
        m_grid.getListStore ().removeAll ();
        m_grid.getListStore ().add (result);
    }

    /**
     * 清除结果
     */
    public void clear ()
    {
        m_grid.clear ();
    }

    private void createGrid ()
    {
        m_grid = new PdsGrid <PdsSupplyDetailModel> ()
        {

            @Override
            public ColumnModel createColumnModel ()
            {
                return getColumnModel ();
            }
        };
        m_grid.getGrid ().setAutoExpandColumn ("usageCodName");
        m_grid.getGrid ().setColumnLines (true);
        m_grid.getGrid ().getView ().setAutoFill (true);
        add (m_grid);
    }

    private GridCellRenderer <ModelData> createPriceRenderer ()
    {
        return new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (final ModelData model, final String property, final ColumnData config, final int rowIndex, final int colIndex, final ListStore <ModelData> store,
                                  final Grid <ModelData> grid)
            {

                final Double costs = model.get ("costs");
                Double price = 0D;
                if (null != costs)
                {
                    price = costs;
                }
                model.set (SupplyStatusQueryDetailGrid.MONEY_KEY, SimpleMethod.retainDoubleTwo (price));
                return "<span style='float:right'>" + model.get (SupplyStatusQueryDetailGrid.MONEY_KEY) + "</span>";
            }
        };
    }

    /**
     * 创建Grid列信息
     * 
     * @return
     */
    private ColumnModel getColumnModel ()
    {
        final List <ColumnConfig> configs = new ArrayList <ColumnConfig> ();
        ColumnConfig column = null;
        final RowNumberer rowNumberer = new RowNumberer ();
        rowNumberer.setHeader ("序号");
        rowNumberer.setWidth (40);
        configs.add (rowNumberer);
        column = new ColumnConfig ("phamName", "药品名称", 150);
        configs.add (column);
        column = new ColumnConfig ("phamSpec", "药品规格", 150);
        configs.add (column);
        column = new ColumnConfig ("unitQty", "包装数量", 60);
        column.setRenderer (ClientUtils.getRenderer ("unitQty"));
        configs.add (column);
        column = new ColumnConfig ("packageUnitName", "包装单位", 60);
        configs.add (column);

        column = new ColumnConfig (SupplyStatusQueryDetailGrid.MONEY_KEY, "金额", 40);
        column.setRenderer (createPriceRenderer ());
        configs.add (column);
        column = new ColumnConfig ("usageCodName", "服法", 120);
        column.setRenderer (ClientUtils.createUsageRenderer ());
        configs.add (column);
        return new ColumnModel (configs);
    }

    /**
     * 初始化UI
     */
    private void initUI ()
    {
        this.setBorders (true);
        this.setLayout (new FitLayout ());
        createGrid ();
    }
}
